package com.self.eduorder.controller;


import com.self.commonutils.R;
import com.self.eduorder.service.PayLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author xiaozeng
 * @since 2020-10-09
 */
@RestController
@RequestMapping("/eduorder/paylog")
@CrossOrigin
public class PayLogController {

    @Autowired
    private PayLogService payLogService;

    //生成微信支付二维码接口
    @GetMapping("createNative/{orderNo}")
    public R createNative(@PathVariable("orderNo") String orderNo){

        //返回信息：包含二维码地址，和其他信息
        Map map = payLogService.createNative(orderNo);
        System.out.println("信息返回Map = " + map);
        return R.ok().data(map);
    }

    //获取支付状态接口
    @GetMapping("queryPayStatus/{orderNo}")
    public R queryPayStatus(@PathVariable("orderNo") String orderNo){

        Map<String,String> map = payLogService.queryPayStatus(orderNo);
        if(map == null){
            return R.error().message("支付出错了~~");
        }
        //如果map不为null，通过map获取订单状态
        if(map.get("trade_state").equals("SUCCESS")){
            //添加记录到支付表，更新订单表状态
            payLogService.updateOrderStatus(map);
            return R.ok().message("支付成功~~");
        }
        return R.ok().code(25000).message("支付中~~");
    }

}

